<?php

/**
* for get ajax data for specify ctrl name
* the template name format "ajax_$func_$ctrl.htm"
*/
function VoteDataCenter_ajax_list() {
    //$ctrl the class name
    $ctrl    = FormUtil::getPassedValue ('ctrl', false , 'GET');

    //$method the method of request for edit or view enum[ view | form | delete | list | page]
    $func  = FormUtil::getPassedValue ('func', 'list' , 'GET');
    //pagnigation variable
    $filter  = FormUtil::getPassedValue ('filter', 0);
    $offset  = FormUtil::getPassedValue ('startnum', 0);
    $sort    = FormUtil::getPassedValue ('sort', '');


    $pagesize = FormUtil::getPassedValue ('pagesize', 100 , 'GET');
    $render = pnRender::getInstance('VoteDataCenter');
    $render->assign ('_GET', $_GET);
    $render->assign ('_POST', $_POST);
    $render->assign ('_REQUEST', $_REQUEST);

    if ($class = Loader::loadClassFromModule ('VoteDataCenter', 'Ajax' . ucfirst($ctrl), true)){

      $objectArray = new $class ();
      $where   = null;
      $sort = null;
      if (method_exists($objectArray,'genPermission')){
        $permit = $objectArray->genPermission();
        if (empty($permit)){
          return LogUtil::registerError ("ส่วนนี้ถูกจำกัดให้ใช้ได้เฉพาะสมาชิกเท่านั้น");
        }
      }
      if (method_exists($objectArray,'genFilter')){
        $where = $objectArray->genFilter();
      }

      if (method_exists($objectArray,'genSort')){
        $sort = $objectArray->genSort();
      }
      if (method_exists($objectArray,'selectExtendResult')){
        $resultex = $objectArray->selectExtendResult();
        $render->assign('extendResult', $resultex);
      }
      if (empty($where)){
        $where = $objectArray->_objWhere;
      }
      if (empty($sort)){
        $sort = $objectArray->_objSort;
      }
      $objectArray->get ($where, $sort , $offset, $pagesize);
      //var_dump($objectArray);
      //assign to view
      $render->assign('objectArray', $objectArray->_objData);
    }
    $render->assign('ctrl', $ctrl);

    if ($lang){
      $render->assign('lang', $lang);
    }else{
      $render->assign('lang', pnUserGetLang());
    }
    echo $render->fetch('ajax_'.$func.'_'.strtolower($ctrl).'.htm');
    return true;
}

  function fixEncoding($in_str) { 
    $cur_encoding = mb_detect_encoding($in_str) ; 
    /*
      $mb_encoding  = mb_check_encoding($in_str,"UTF-8");
      return $cur_encoding."<br>mb :".$mb_encoding;
    */
    if($cur_encoding == "UTF-8" && mb_check_encoding($in_str,"UTF-8")) 
      return $in_str; 
    else 
      return utf8_encode($in_str); 

  } // fixEncoding 